#include "sc_ui_menu.h"
#include "SCGUI/sc_gui.h"


static SC_tile pfb= {0};     //
static int scc_key = 0;

// 菜单项结构体
typedef struct menu_t
{
    char* name;
    uint8_t len;
    uint8_t cursor;
    struct menu_t* parent_menu; // 父菜单
    struct menu_t* sub_menu; // 子菜单
    void (*key_event)(int *event);
} MenuItem;



extern MenuItem main_page[];
extern MenuItem MenuItems2[];
extern MenuItem MenuItems3_1[];
extern MenuItem MenuItems3_2[];

MenuItem *g_menu = main_page; // 当前菜单

int menu_cursor_up(MenuItem *menu)
{
    if(menu->cursor == 0)
    {
        menu->cursor = menu->len - 1;
        return 1;
    }

    if (menu->cursor > 0)
    {
        menu->cursor--;
        return 1;
    }
    return 0;
}
int menu_cursor_dw(MenuItem *menu)
{

    if(menu->cursor == menu->len - 1)
    {
        menu->cursor = 0;
        return 1;
    }

    if (menu->cursor < menu->len - 1)
    {
        menu->cursor++;
        return 1;
    }
    return 0;
}

void menu_event(int *event)
{
    switch (*event)
    {
    case 1:
        /* code */
        menu_cursor_up(g_menu);
        break;
    case 2:
        /* code */
        menu_cursor_dw(g_menu);
        break;
    case 5:
        /* code */

        if(g_menu[g_menu->cursor].sub_menu != NULL)
        {
            g_menu = g_menu[g_menu->cursor].sub_menu;
            printf(">enter sub menu\n");
        }
        else
        {
            printf("*no sub menu*\n");
        }

        break;
    case 6:
        /* code */
        if(g_menu->parent_menu != NULL)
        {
            g_menu = g_menu->parent_menu;
            printf(">back to parent menu\n");
        }
        else
        {
            printf("*no parent menu*\n");
        }


        break;

    default:
        break;
    }
}


// 声明函数
void main_page_key_event(int* event)
{
    //printf("main_page_key_event %d\n", *event);

    menu_event(event);

    *event = 0;
}
void menu2_key_event(int* event)
{
    //printf("menu2_key_event %d\n", *event);

    menu_event(event);

    *event = 0;
}
void menu3_1_key_event(int* event)
{
    //printf("menu3_1_key_event %d\n", *event);

    menu_event(event);

    *event = 0;
}
void menu3_2_key_event(int* event)
{
    //printf("menu3_2_key_event %d\n", *event);

    menu_event(event);

    *event = 0;
}



// 三级子菜单
MenuItem MenuItems3_1[] = {
    {"menu31",   5, 0, MenuItems2, NULL,menu3_1_key_event},
    {"aa-1", 0, 0, MenuItems2, NULL,menu3_1_key_event},
    {"bbbb-2", 0, 0, MenuItems2, NULL,menu3_1_key_event},
    {"ccccc-3", 0, 0, MenuItems2, NULL,menu3_1_key_event},
    {"dd-4", 0, 0, MenuItems2, NULL,menu3_1_key_event},
};

MenuItem MenuItems3_2[] = {
    {"menu32", 3, 0, MenuItems2, NULL,menu3_2_key_event},
    {"aa-1", 0, 0, MenuItems2, NULL,menu3_2_key_event},
    {"ddddd-2", 0, 0, MenuItems2, NULL,menu3_2_key_event},
};


// 二级子菜单
MenuItem MenuItems2[] = {
    {"menu2", 3, 0, main_page, MenuItems3_1,menu3_2_key_event},
    {"aa-1", 3, 0, main_page, MenuItems3_1,menu2_key_event},
    {"cccccc-2", 3, 0, main_page, MenuItems3_2,menu2_key_event},
};

// 主页面
MenuItem main_page[] =
{
    {"Menu",  1, 0, NULL, MenuItems2,main_page_key_event},
};

int get_name_len(char * name)
{
    int len =0;
    while (*name) {
        len++;
        name++;
    }
    return len;
}
static int corsor_y = 0;
static int corsor_ty = 0;
static int corsor_w = 0;
static int corsor_tw = 0;
// 显示菜单
void displayMenu(const MenuItem* menu)
{
    //printf("menu %x\n",menu);
    // if(menu->name=="Menu")
    // {
    //     printf("Menu\n");
    //     printf("input key in menu\n");
    // }
    // else
    int x = 80;
    int y = 10;

    {
        //printf("cursor %d\n",menu[0].cursor);
        for (int i = 0; i< menu[0].len; ++i)
        {
            if(i==menu[0].cursor)
            {
                //printf("> %s\n", menu[i].name);
                SC_label label;
                SC_set_label(&label,x,y,80,30,menu[i].name,STR_ALIGN_LEFT);
                SC_pfb_label(&pfb,&label,0,4,C_RED,gui->bkc,gui->font);

                corsor_ty = y;
                corsor_tw = get_name_len(menu[i].name) * 10;

                printf("corsor_ty %d corsor_tw %d\r\n",corsor_ty,corsor_tw);

                SC_label label_cursor;
                SC_set_label(&label_cursor,x-30,corsor_y,80,30,">",STR_ALIGN_LEFT);
                SC_pfb_label(&pfb,&label_cursor,0,4,C_RED,gui->bkc,gui->font);

                SC_pfb_DrawFrame(&pfb,x-5,corsor_y,x-5+corsor_w,30+corsor_y,C_BLUE);

            }
            else
            {
                SC_label label;
                SC_set_label(&label,x,y,80,30,menu[i].name,STR_ALIGN_LEFT);
                SC_pfb_label(&pfb,&label,0,4,C_WHITE,gui->bkc,gui->font);
            }

            y+=30;

        }

        if(corsor_y < corsor_ty)
            corsor_y += 1;
        if(corsor_y > corsor_ty)
            corsor_y -= 1;
        if(corsor_w < corsor_tw)
            corsor_w += 1;
        if(corsor_w > corsor_tw)
            corsor_w -= 1;

    }

}



void set_key(int key)
{
    scc_key = key;
}

void menu_test()
{
    do
    {
        SC_pfb_clip(&pfb,0,0,LCD_SCREEN_WIDTH-1,LCD_SCREEN_HEIGHT-1,gui->bkc);

        displayMenu(g_menu);

        g_menu->key_event(&scc_key);

    }while(SC_pfb_Refresh(&pfb,0)&& 1);
}

